package cn.ekuma.android.ui.tree;

import java.util.ArrayList;
import java.util.List;

public  class TreeElement {
         private String title;
         private int level;
         private TreeElement parent;
         private boolean expanded = false;
         private Object tag;
         private boolean selected;
         private List<TreeElement> children = new ArrayList<TreeElement>();

        public TreeElement(String title, Object tag) {
             this.title = title;
             this.tag = tag;
         }

         public void addChild(TreeElement child) {
             children.add(child);
             child.setParent(this);
         }  

         public int childCount() {
             return children.size();
         }
 
         public boolean hasChildren() {
             if (children.size() > 0)
                 return true;
             return false;
         }
 
         public TreeElement getChild(int position) {
             if (position < 0 || position > children.size() - 1)
                 return null;
            return children.get(position);
         }
 
         public String getTitle() {
             return title;
        }
 
        public void setTitle(String title) {
            this.title = title;
        }

         /**
         * ��ǰԪ�صĵȼ���Ĭ��Ϊ1����padding = 25 * level
         * 
          * @return
 */
        protected int getLevel() {
         level = 1;
           TreeElement parent = this.getParent();
             while (parent != null) {
                parent = parent.getParent();
                level++;
            }
             return level;
         }
 
       protected boolean isExpanded() {
             return expanded;
        }
 
         protected void setExpanded(boolean expanded) {
             this.expanded = expanded;
       }

         public TreeElement getParent() {
            return parent;
         }
 
         public void setParent(TreeElement parent) {
            this.parent = parent;
       }
 
        public Object getTag() {
            return tag;
         }

         public void setTag(Object tag) {
            this.tag = tag;
        }

        public boolean isSelected() {
            return selected;
         }

        public void setSelected(boolean selected) {
             this.selected = selected;
        }

		public List<TreeElement> getChildren() {
			return children;
		}

		public void setChildren(List<TreeElement> children) {
			this.children = children;
		}

		public void setLevel(int level) {
			this.level = level;
		}
		
	    public static TreeElement createElement(String title) {
	         return new TreeElement(title, null);
	     }
	 
	     public static TreeElement createElement(String title, Object tag) {
	         return new TreeElement(title, tag);
	     }
	 
	     public static TreeElement createElement(String title, Object tag,
	             TreeElement parent) {
	         TreeElement e = new TreeElement(title, tag);
	        if (parent != null)
	            parent.addChild(e);
	         return e;
	     }

	     public static TreeElement createElement(String title, TreeElement parent) {
	         return createElement(title, null, parent);
	     }
    }
